#include "sh.h"
#include <cstdio>
void SHLineClip(Polygon<val_t> region, Polygon<val_t> &pol)
{
    Polygon<val_t> clipped = pol;
    Polygon<val_t> tmp;

    // 可见为1,不可见为0,低位为后面的点
    unsigned char insideFlag = 0;
    auto vertex_iter = clipped.vertices.begin();
    // 起点 S
    auto vertex_last = *vertex_iter;
    // 终点 P
    auto vertex_now = vertex_last;
    Point<val_t> cross;
    Line<val_t> edge;
    auto edgeList = region.getEdges();
    // 遍历边
    for (auto iter = edgeList.begin(); iter != edgeList.end(); ++iter)
    {
        edge = *iter;
        insideFlag = 0;

        log("遍历边(%g,%g)->(%g,%g)\n", edge.start.x, edge.start.y, edge.end.x, edge.end.y);

        // 遍历点,求交点
        for (++vertex_iter; vertex_iter != clipped.vertices.end(); )
        {
            vertex_now = *(vertex_iter);

            log("进入边(%g,%g)->(%g,%g)\n", vertex_last.x, edge.start.y, vertex_now.x, vertex_now.y);
            if (edge.A == 0)
            { // 平行x轴
                if (edge.start.y == region.bottom)
                {
                    // 上面为可见侧
                    insideFlag = vertex_last.y > region.bottom ? 1 : 0;
                    insideFlag <<= 1;
                    insideFlag = vertex_now.y > region.bottom ? 1 : 0;
                }
                else
                {
                    // 下面为可见侧
                    insideFlag = vertex_last.y < region.top ? 1 : 0;
                    insideFlag <<= 1;
                    insideFlag = vertex_now.y < region.top ? 1 : 0;
                }
            }
            else if (edge.B == 0)
            { // 平行y轴
                if (edge.start.x == region.left)
                {
                    // 右边为可见侧
                    insideFlag = vertex_last.x > region.left ? 1 : 0;
                    insideFlag <<= 1;
                    insideFlag = vertex_now.x > region.left ? 1 : 0;
                }
                else
                {
                    // 左边为可见侧
                    insideFlag = vertex_last.x < region.right ? 1 : 0;
                    insideFlag <<= 1;
                    insideFlag = vertex_now.x < region.right ? 1 : 0;
                }
            }
            Line<double> _line(vertex_last, vertex_now);

            switch (insideFlag)
            {
            case 0:
                // 0b00都在不可见侧,不输出
                log("%d 都在不可见侧,不输出 \n", insideFlag);

                break;
            case 1:
                // 0b01 不可见侧->可见侧,输出 交点和终点
                cross = edge.A == 0 ? _line.get_point_at_y(edge.start.y) : _line.get_point_at_x(edge.start.x);
                // clipped.addPoint(vertex_now);
                tmp.addPoint(cross);
                tmp.addPoint(vertex_last);
                log("%d 不可见侧->可见侧,输出 交点(%g,%g)和终点(%g,%g)\n", insideFlag, cross.x, cross.y, vertex_now.x, vertex_now.y);

                break;
            case 2:
                // 0b10 可见侧->不可见侧 输出交点
                cross = edge.A == 0 ? _line.get_point_at_y(edge.start.y) : _line.get_point_at_x(edge.start.x);
                tmp.addPoint(cross);
                log("%d 可见侧->不可见侧 输出交点 (%d,%d)\n", insideFlag);
                break;
            case 3:
                // 0b11 都在可见侧,输出终点
                tmp.addPoint(vertex_now);

                log("%d 都在可见侧,输出终点(%d,%d) \n", insideFlag, vertex_now.x, vertex_now.y);

                break;

            default:
                break;
            }
            vertex_last = vertex_now;
        }
        tmp.drawEdge();
        clipped = tmp;
        getchar();
    }
}